package cn.jbolt.ec.order;

import cn.hutool.core.util.StrUtil;
import cn.jbolt.common.model.*;
import cn.jbolt.ec.rush.RushCommodityService;
import cn.jbolt.ec.wx.MsgService;
import com.jfinal.aop.Inject;
import com.jfinal.log.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderMsgService{

    @Inject
    OrderCommodityService orderCommodityService;
    @Inject
    RushCommodityService rushCommodityService;

    /**
     * RLXZJpppODhKQ-k9PiHJikwmjlIYQG1B0q9SLmv8D_A
     * 开发者调用模版消息接口时需提供模版ID
     * 标题发货通知
     * 行业IT科技 - 互联网|电子商务
     * 详细内容{{first.DATA}}
     * 订单编号：{{keyword1.DATA}}
     * 订单商品：{{keyword2.DATA}}
     * 买家昵称：{{keyword3.DATA}}
     * 付款时间：{{keyword4.DATA}}
     * {{remark.DATA}}
     *
     */
    public void notify_retailer(BecRetailerOrder becRetailerOrder){

        if (1==becRetailerOrder.getType()||14==becRetailerOrder.getType()||15==becRetailerOrder.getType()){
            System.out.println("通知店员");
            BecRetailer becRetailer= new BecRetailer().dao().findById(becRetailerOrder.getRetailerId());
            String first=becRetailer.getName();
            String keyword1= becRetailerOrder.getOrderId();
            List<String> stringList0 = getCommodities(becRetailerOrder);
            String keyword2= StrUtil.removeSuffix(StrUtil.removePrefix(stringList0.toString(),"["),"]").replaceAll(",","");
            String keyword3=becRetailerOrder.getConsignee();
            //时间转换
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(becRetailerOrder.getOrderTime());
            String keyword4 = simpleDateFormat.format(date);
            String remark=becRetailerOrder.getMobile();
            List<BecRetailerStaff> list = new BecRetailerStaff().dao().find("select openid from bec_retailer_staff where retailer_id=?",becRetailerOrder.getRetailerId());
            //遍历取得商家店员openid
            List<String> stringList=new ArrayList<>();
            for (int i = 0; i <list.size() ; i++) {
                stringList.add(list.get(i).getOpenid());
            }
            for (int i = 0; i <stringList.size() ; i++) {
                if (becRetailerOrder.getChargeId()>0){
                    List<BecRushstoreCommodity> stringList1  = rushCommodityService.rush_all_commodity(becRetailerOrder.getChargeId());
                    String keyword0= "数量:1"+"\n"+"商品名:"+stringList1.get(0).getCommodityName()+"\n"+"商品规格:"+stringList1.get(0).getOptionTitle()+"\n";
                    if (14==becRetailerOrder.getType()){
                        MsgService.me.sendMsg(MsgTempService.notify_retailer,stringList.get(i),first+"  积分订单商品",keyword1,keyword0,keyword3,keyword4,"",remark);
                    }else if (15==becRetailerOrder.getType()){
                        MsgService.me.sendMsg(MsgTempService.notify_retailer,stringList.get(i),first+"  秒杀订单商品",keyword1,keyword0,keyword3,keyword4,"",remark);
                    }else {
                        MsgService.me.sendMsg(MsgTempService.notify_retailer,stringList.get(i),first,keyword1,keyword0,keyword3,keyword4,"",remark);
                    }

                }else {
                        MsgService.me.sendMsg(MsgTempService.notify_retailer,stringList.get(i),first,keyword1,keyword2,keyword3,keyword4,"",remark);
                }

            }
        }

    }

    /**
     * 标题分销成功提醒
     * 行业IT科技 - 互联网|电子商务
     * 详细内容{{first.DATA}}
     * 商品信息：{{keyword1.DATA}}
     * 商品单价：{{keyword2.DATA}}
     * 商品佣金：{{keyword3.DATA}}
     * 分销时间：{{keyword4.DATA}}
     * {{remark.DATA}}
     * @param becRetailerOrder
     */
    public void notify_spreader(BecRetailerOrder becRetailerOrder){

        BecMember becMember= new BecMember().dao().findById(becRetailerOrder.getClientId());
        if (becRetailerOrder.getCredit()>0){
            if (null!=becMember.getSpreaderId()){
                BecMember becMemberSpreader= new BecMember().dao().findById(becMember.getSpreaderId());
                List<String> stringList0 = getCommodities(becRetailerOrder);
                String first="分销成功提醒";
                String keyword1= StrUtil.removeSuffix(StrUtil.removePrefix(stringList0.toString(),"["),"]").replaceAll(",","");
                double d= becRetailerOrder.getAmountPaid()/(double)100;
                String keyword2=String.format("%.2f", d);
                double credit=becRetailerOrder.getCredit()/(double)100;
                String keyword3=String.format("%.2f",credit)+"元";
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String keyword4 = dateFormat.format(System.currentTimeMillis());
                MsgService.me.sendMsg(MsgTempService.notify_spreader,becMemberSpreader.getOpenidJmd()
                        ,first,keyword1,keyword2,keyword3,keyword4,"","");
            }
        }
    }

    /**
     * 遍历取得商品
     * @param becRetailerOrder
     * @return
     */
    private List<String> getCommodities(BecRetailerOrder becRetailerOrder) {
        List<String> stringList0 = new ArrayList<>();
        List<BecRetailerOrderCommodity> commodities = orderCommodityService.get_order_commodity(becRetailerOrder.getId());
        for (BecRetailerOrderCommodity commodity : commodities) {
            Integer a = commodity.get("amount");
            String b = commodity.get("option_title");
            if (null==b) b="无";
            String c = commodity.get("commodity_name");
            stringList0.add("数量:" + a + "\n" + "商品名:" + c + "\n" + "规格:" + b + "\n");
        }
        return stringList0;
    }

    /**
     * _XlDxjUFPfBTEAm3mzyE1k0uidFZG1J8GpPH3KwJ16A
     {{first.DATA}}
     支付金额：{{keyword1.DATA}}
     商品详情：{{keyword2.DATA}}
     订单编号：{{keyword3.DATA}}
     备注：{{keyword4.DATA}}
     {{remark.DATA}}
     * @param becRetailerOrder
     */
    public void notify_member(BecRetailerOrder becRetailerOrder){
        if (becRetailerOrder.getType()==1){
            List<String> stringList0 = getCommodities(becRetailerOrder);
            String first="收款商家:"+ new BecRetailer().dao().findById(becRetailerOrder.getRetailerId()).getName();
            double d= becRetailerOrder.getAmountPaid()/(double)100;
            String keyword1=String.format("%.2f", d);
            String keyword2= StrUtil.removeSuffix(
                    StrUtil.removePrefix(stringList0.toString(),
                            "["),"]")
                    .replaceAll(",","");
            String keyword3=becRetailerOrder.getOrderId();
            String openid = new BecMember().dao().findById(becRetailerOrder.getClientId()).getOpenidJmd();
            MsgService.me.sendMsg(MsgTempService.notify_member,openid,first,keyword1,keyword2,keyword3,"","","您已成功付款");
        }

    }


    /**
     * -G6NBJKV45-iRIJ3zxyD5bpZdFLgLxrTtr6-Iegojk4
     * 开发者调用模版消息接口时需提供模版ID
     * 详细内容
     * {{first.DATA}}
     * 订单号：{{keyword1.DATA}}
     * 订单金额：{{keyword2.DATA}}
     * 买家：{{keyword3.DATA}}
     * 订单状态：{{keyword4.DATA}}
     * {{remark.DATA}}
     */
    public void spike_order_notify(BecRetailerOrder becRetailerOrder){

        List<BecRushstoreCommodity> stringList0  = rushCommodityService.rush_all_commodity(becRetailerOrder.getChargeId());
        String keyword1=becRetailerOrder.getOrderId();
        String keyword3=new BecMember().dao().findById(becRetailerOrder.getClientId()).getNickname();
        String keyword4="已支付";

        if (14==becRetailerOrder.getType()){

            String first="您有一个积分商品订单";
            double d= stringList0.get(0).getCreditPrice()/(double)100;
            if (d>=1){
                String keyword2=String.format("%.0f", d)+"积分";
                String openid = new BecMember().dao().findById(becRetailerOrder.getClientId()).getOpenidJmd();
                MsgService.me.sendMsg(MsgTempService.spike_order_notify_spreader,openid,first,keyword1,keyword2,keyword3,keyword4,"","");
            }
        }else if (15==becRetailerOrder.getType()){

            String first="您有一个秒杀商品订单";
            double d= stringList0.get(0).getRushPrice()/(double)100;
            String keyword2=String.format("%.2f", d)+"元";
            String openid = new BecMember().dao().findById(becRetailerOrder.getClientId()).getOpenidJmd();
            MsgService.me.sendMsg(MsgTempService.spike_order_notify_spreader,openid,first,keyword1,keyword2,keyword3,keyword4,"","");
        }


    }

}
